// pages/childattendanceclassstudent/childattendanceclassstudent.js
const app = getApp()
var util = require("../../utils/util.js");
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classid: 0,
    classlist: [],
    kidlist:[],
    currentyear: null,
    currentmonth: null,
    year: null,
    month: null,
    selectdate: util.formatDate(new Date()),
    startdate: util.formatDateMonth(new Date('2000-01-01')),
    enddate: util.formatDateMonth(new Date()),
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var classid = 0 
    var list = wx.getStorageSync('classlist');
    var classlist = []
    var classobj = {
      text: "全部班级月度报表",
      value: 0
    }
    classlist.push(classobj)
    for (var i = 0; i < list.length; i++) {
      var classobj = {
        text: list[i].className+"月度报表",
        value: list[i].classID
      }
      classlist.push(classobj)
    }
    this.setData({
      classid: classlist[0].value,
      classlist:classlist
    })


    var year = new Date().getFullYear()
    var month = new Date().getMonth() + 1
    var month1 =parseInt(month)
    if (month1 < 10) {
      month1 = '0' + month1;
    }
    var selectdate = year + "-" + month1
    this.setData({
      year: parseInt(year),
      month: parseInt(month),
      currentyear: parseInt(year),
      currentmonth: parseInt(month),
      selectdate: selectdate,
    })
    console.log("classid===" + classid)
    console.log("year===" + year)
    console.log("month===" + month)
    this.queryData()
  },
  onchange({
    detail
  }) {
    this.setData({
      classid: detail
    });
    this.queryData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  queryData: function () {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "year": that.data.year,
      "month": that.data.month,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_kidmonthreport"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            kidlist: res.data.data,
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },


  bindDateChange: function (e) {
    console.log(e)
    var selectdate= e.detail.value
    var year = selectdate.substring(0,4)
    var month = selectdate.substring(5,7)
    this.setData({
      year: parseInt(year),
      month: parseInt(month),
      selectdate: selectdate
    })
    this.queryData();
  },


  btnPreday: function () {
    var year = this.data.year
    var month = this.data.month

    if (month == 1) {
      month = 12
      year = year - 1
    } else {
      month = month - 1;
    }
    var month1 = month
    if (month1 < 10) {
      month1 = '0' + month1;
    }
    var selectdate = year + "-" + month1
    this.setData({
      year: year,
      month: month,
      selectdate: selectdate
    })
    this.queryData();
  },

  btnNextday: function () {

    var year = this.data.year
    var month = this.data.month
    if (year == this.data.currentyear && month == this.data.currentmonth) {
      wx.showToast({
        title: '还未到达得下个月',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {
      if (month == 12) {
        month = 1
        year = year + 1
      } else {
        month = month + 1;
      }
      var month1 = month
      if (month1 < 10) {
        month1 = '0' + month1;
      }
      var selectdate = year + "-" + month1
      this.setData({
        year: year,
        month: month,
        selectdate: selectdate
      })

    }

    this.queryData();


  },



  
})